<template>
  <div class="breadcrumb-container">
    <el-breadcrumb :separator-icon="ArrowRight">
      <el-breadcrumb-item :to="{ path: '/' }">{{ t('common.home') }}</el-breadcrumb-item>
      <template v-if="(route.name === 'question' || route.name === 'questionDetail') && route.query.subjectName">
        <el-breadcrumb-item :to="{ path: '/subject' }">{{ t('common.subjectManagement') }}</el-breadcrumb-item>
        <el-breadcrumb-item 
          :to="{ 
            path: '/question',
            query: { 
              subjectId: route.query.subjectId,
              subjectName: route.query.subjectName
            }
          }"
        >
          {{ route.query.subjectName }}
        </el-breadcrumb-item>
        <el-breadcrumb-item v-if="route.name === 'questionDetail'">
          {{ route.query.libraryName }}
        </el-breadcrumb-item>
      </template>
    
      <template v-else>
        <el-breadcrumb-item 
          v-for="(item, index) in breadcrumbList" 
          :key="index" 
          :to="index === breadcrumbList.length - 1 ? null : item.path"
        >
          {{ t(item.meta?.i18n || item.meta?.title) }}
        </el-breadcrumb-item>
      </template>
    </el-breadcrumb>

    <!-- 语言选择器 -->
    <!-- <div class="language-selector">
      <el-select
        v-model="currentLanguage"
        class="language-select"
        size="small"
        @change="handleLanguageChange"
      >
        <el-option
          v-for="item in languages"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
          <span class="language-option">
            <i :class="item.icon" class="language-icon"></i>
            {{ item.label }}
          </span>
        </el-option>
      </el-select>
    </div> -->
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useI18n } from 'vue-i18n'
import { ArrowRight } from '@element-plus/icons-vue'
import 'flag-icons/css/flag-icons.min.css'

const route = useRoute()
const router = useRouter()
const { t, locale } = useI18n()
const breadcrumbList = ref<any[]>([])

// 语言选择相关
const currentLanguage = ref(locale.value)
const languages = [
  { value: 'zh-CN', label: '简体中文', icon: 'fi fi-cn' },
  { value: 'en-US', label: 'English', icon: 'fi fi-us' },
  { value: 'ja-JP', label: '日本語', icon: 'fi fi-jp' }
]

const handleLanguageChange = (value: string) => {
  locale.value = value
  currentLanguage.value = value
  localStorage.setItem('language', value)
  // 可选：刷新页面以确保所有组件都更新语言
  // window.location.reload()
}

const getBreadcrumb = () => {
  // 过滤掉没有meta.title的路由
  const matched = route.matched.filter(item => item.meta && (item.meta.title || item.meta.i18n))
  
  // 如果是首页，不显示面包屑
  if (matched.length === 1 && matched[0].path === '/') {
    breadcrumbList.value = []
    return
  }
  
  // 如果是题库管理页面或题库详情页，特殊处理
  if ((route.name === 'question' || route.name === 'questionDetail') && route.query.subjectName) {
    return
  }
  
  // 过滤掉首页路由，因为我们已经手动添加了首页
  breadcrumbList.value = matched.filter(item => item.path !== '/')
}

// 监听路由变化
watch(
  () => route.path,
  () => {
    getBreadcrumb()
  },
  {
    immediate: true
  }
)

// 同时监听query变化
watch(
  () => route.query,
  () => {
    getBreadcrumb()
  }
)

// 监听语言变化
watch(
  () => locale.value,
  () => {
    getBreadcrumb()
  }
)
</script>

<style scoped>
.breadcrumb-container {
  padding: 0 24px;
  font-size: 14px;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.language-selector {
  margin-left: auto;
}

.language-select {
  width: 120px;
}

.language-option {
  display: flex;
  align-items: center;
  gap: 8px;
}

.language-icon {
  font-size: 16px;
}

:deep(.el-breadcrumb__item) {
  display: inline-flex;
  align-items: center;
}

:deep(.el-breadcrumb__inner) {
  color: #909399;
  font-weight: normal;
  transition: all 0.3s ease;
}

:deep(.el-breadcrumb__inner.is-link) {
  color: #606266;
  cursor: pointer;
}

:deep(.el-breadcrumb__inner.is-link:hover) {
  color: #409EFF;
  text-decoration: none;
}

:deep(.el-breadcrumb__separator) {
  margin: 0 8px;
  color: #c0c4cc;
  font-weight: normal;
}

:deep(.el-breadcrumb__item:last-child .el-breadcrumb__inner) {
  color: #303133;
  cursor: default;
  font-weight: 500;
}

:deep(.el-breadcrumb__item:last-child .el-breadcrumb__inner:hover) {
  color: #303133;
}
</style> 